<?php

namespace app\index\controller;

use app\common\validate\Test as TestValidate; // 校验器

use think\Controller;
use think\Db;
use think\Request;




class Test extends Controller
{

    //前台代码
    public function index()
    {
        return $this->fetch();
    }

    // task考试任务
    public function tasklist()
    {
        return $this->fetch();
    }

    public function gettasklist()
    {
        $map['schoolid'] = array('eq', 985);
        input('get.id') && $map['id']  = array('eq', input('get.id'));
        input('get.name') && $map['name']  = array('like', '%' . input('get.name') . '%');
        input('get.term') && $map['term']  = array('eq', input('get.term'));
        input('get.create_time') && $map['create_time']  = array('like', '%' . input('get.create_time') . '%');
        $result =  Db::name('task')->where($map)->order('id')->select();
        // echo Db::name('task')->getLastSql();
        return json($result);
    }

    public function taskadd()
    {
        if (request()->isAjax()) {
            $data = [
                'schoolid' => input('post.schoolid'),
                'name' => input('post.name'),
                'term' => input('post.term'),
                'uid' => input('post.uid'),
                'create_time' => input('post.create_time')
            ];

            $validate = new TestValidate;

            if (!$validate->scene('taskadd')->check($data)) {
                return $validate->getError();
            }

            $result =  Db::name('task')->insert($data);

            if ($result !== false) {
                $this->success('考试任务添加成功！', 'test/tasklist');
            } else {
                $this->error($result);
            }
        }
        return view();
    }

    public function taskedit()
    {
        if (Request::instance()->isGet()) {
            $result =  Db::name('task')->find(input('get.id'));
            if ($result !== false) {
                $this->success('成功！', '', $result);
            } else {
                $this->error($result);
            }
        }

        if (Request::instance()->isPost()) {
            $data = [
                'id' => input('post.id'),
                'schoolid' => input('post.schoolid'),
                'name' => input('post.name'),
                'term' => input('post.term'),
                'uid' => input('post.uid')
            ];

            $result = Db::name('task')
                ->where('id', $data['id'])
                ->update(['name' => $data['name'], 'term' => $data['term']]);

            if ($result !== false) {
                $this->success('考试任务编辑成功！', 'test/tasklist');
            } else {
                $this->error($result);
            }
        }
        return view();
    }

    // 任务删除
    public function taskremove()
    {
        if (Request::instance()->isGet()) {
            $result =  Db::name('task')->where('id', input('get.id'))
                ->delete();
            if ($result !== false) {
                $this->success('删除成功！', 'test/tasklist');
            } else {
                $this->error($result);
            }
        } else {
            $ids = input('post.ids');
            $res = Db::name('task')->delete($ids);
            if ($res) {
                $this->success('删除成功！', 'test/tasklist');
            } else {
                $this->error('删除失败');
            }
        }
    }

    /* 考试课程模块 */
    public function courselist()
    {

        $taskListData =  Db::name('task')->where('schoolid', 985)->order('id')->paginate(5);
        $this->assign('taskdata', $taskListData);

        $kechengListData =  Db::name('kecheng')
            ->where('schoolid', 985)->select();
        $this->assign('kechengdata', $kechengListData);
        return view();
    }

    public function getcourselist()
    {
        $map['a.schoolid'] = array('eq', 985);

        input('get.id') && $map['a.id']  = array('eq', input('get.id'));
        input('get.name') && $map['c.name']  = array('like', '%' . input('get.name') . '%');
        input('get.coursename') && $map['b.coursename']  = array('like', '%' . input('get.coursename') . '%');
        input('get.check') && $map['a.check']  = array('eq', input('get.check'));
        input('get.create_time') && $map['a.create_time']  = array('like', '%' . input('get.create_time') . '%');

        if (input('get.ischeck')) {
            $map['a.check'] = array('eq', 1);
        }

        $courseListData =  Db::name('course')
            ->alias('a')
            // ->Field('a.id as aid, a.*')
            ->join('task c', 'a.taskid = c.id')
            ->join('kecheng b', 'a.kechengid = b.kechengid', 'LEFT')
            ->field('c.name,b.id as c_id,b.*,a.*')
            ->where($map)->order('a.id')->select();
        return json($courseListData);
    }

    // 编辑
    public function courseedit()
    {
        if (Request::instance()->isGet()) {
            $result =  Db::name('course')->find(input('get.id'));
            if ($result !== false) {
                $this->success('成功！', '',  $result);
            } else {
                $this->error($result);
            }
        }

        if (Request::instance()->isPost()) {
            $data = input('post.');

            $result = Db::name('course')
                ->where('id', $data['id'])
                ->update(['c_test_type' => $data['c_test_type']]);

            if ($result !== false) {
                $this->success('设置成功！', 'test/courselist');
            } else {
                $this->error($result);
            }
        }
        return view();
    }

    // 批量设置考试形式
    public function batchtesttype()
    {

        if (Request::instance()->isPost()) {
            $data = input('post.');
            $ids = json_decode($data['ids'], true);
            foreach ($ids as $item) {
                $result = Db::name('course')
                    ->where('id', $item)
                    ->update(['c_test_type' => $data['c_test_type']]);
            }

            //  $result = Db::name('course')
            //      ->where('id', $data['id'])
            //      ->update(['c_test_type' => $data['c_test_type']]);

            if ($result !== false) {
                $this->success('设置成功！', 'test/courselist');
            } else {
                $this->error($result);
            }
        }
        return view();
    }

    // 考试课程删除
    public function courseremove()
    {
        if (Request::instance()->isGet()) {
            $res =  Db::name('course')->where('id', input('get.id'))
                ->delete();
        } else {
            $ids = input('post.ids');
            $res = Db::name('course')->delete($ids);
        }

        if ($res) {
            $this->success('删除成功！', 'test/courselist');
        } else {
            $this->error('删除失败');
        }
    }

    // 审核
    public function coursecheck()
    {
        if (Request::instance()->isGet()) {
            $result =  Db::name('course')->where('id', input('get.id'))
                ->update(['check' => input('get.check')]);
        } else {
            $ids = json_decode(input('post.ids'), true);
            foreach ($ids as $item) {
                $result = Db::name('course')
                    ->where('id', $item)
                    ->update(['check' => input('post.check')]);
            }
        }

        if ($result !== false) {
            $this->success('操作成功！', 'test/courselist');
        } else {
            $this->error($result);
        }
    }

    /* 监考老师模块 */
    public function teacherlist()
    {
        $taskListData =  Db::name('task')->where('schoolid', 985)->select();
        $this->assign('taskdata', $taskListData);
        $teacherInfoListData =  Db::name('teacher_info')
            ->select();
        $this->assign('teacherinfodata', $teacherInfoListData);
        return view();
    }

    /**
     * 获取本校全部老师列表
     */
    public function getteacherlist()
    {
        $map['schoolid'] = array('eq', 985);

        $offset =  (input('get.page') - 1) * input('get.limit');
        $record =  Db::name('teacher_info')->where($map)->order('id')->limit($offset, input('get.limit'))->select();
        $total =  Db::name('teacher_info')->where($map)->order('id')->select();

        $listData['record'] = $record;
        $listData['total'] = count($total);
        return json($listData);
    }

    /* 监考老师申请 */
    public function teachertestapp()
    {
        $taskListData =  Db::name('task')->where('schoolid', 985)->select();
        $this->assign('taskdata', $taskListData);
        $teacherInfoListData =  Db::name('teacher_info')
            ->select();
        $this->assign('teacherinfodata', $teacherInfoListData);
        return view();
    }

    /**
     * 获取监考老师申请列表
     */
    public function getteachertestapp()
    {
        $map['a.schoolid'] = array('eq', 985);
        $map['a.taskid'] = array('eq', input('get.taskid'));
        $map['a.check'] = array('eq', 0);

        $offset =  (input('get.page') - 1) * input('get.limit');
        $record =  Db::name('test_apply')
            ->alias('a')
            ->join('teacher_info b', 'a.teacherid = b.id')
            ->Field('b.teachername, a.*')
            ->where($map)->order('id')->limit($offset, input('get.limit'))->select();
        $total =  Db::name('test_apply')->alias('a')->where($map)->order('id')->select();

        $listData['record'] = $record;
        $listData['total'] = count($total);
        return json($listData);
    }

    /**
     * 监考老师申请
     */
    public function applyWacthTest()
    {
        if (request()->isAjax()) {
            $data =  input('post.');

            $result =  Db::name('test_apply')->insert([
                        'schoolid' => 985,
                        'teacherid' => $data['teacherid'],
                        'taskid' => $data['taskid'],
                         'demo' =>$data['demo'],
                        'create_time' => date('y-m-d h:i:s', time()),
                    ]);
            if ($result !== false) {
                $this->success('操作成功！', 'test/resultlist');
            } else {
                $this->error($result);
            }
        }
    }

    /**
     * 获取监考老师申请同意
     */
    public function testappcheck()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $ids = json_decode($data['ids'], true);
            foreach ($ids as $item) {
                $result = Db::name('test_apply')
                    ->where('id', $item)
                    ->update(['check' => $data['check']]);
            }

            // 审核通过更新数据库
            $map['taskid'] = array('eq', $data['taskid']);
            $map['check'] = array('eq', 1);
            $isPass = Db::name('test_apply')->where($map)->select();
            if ($isPass) {
                for ($i = 0; $i < count($isPass); $i++) {

                    $newdata[]  = [
                        'schoolid' => 985,
                        'teacherid' => $isPass[$i]['teacherid'],
                        'taskid' => $isPass[$i]['taskid'],
                        'demo' => '监考老师',       // 备注
                        'create_time' => date('y-m-d h:i:s', time()),
                        'uid' => 1,
                        'role' => 1,
                    ];
                }
                Db::name('teacher')->insertAll($newdata);
            }

            // $result =  Db::name('test_apply')->where($newdata)->update(['check' => $data['check']]);

            if ($result !== false) {
                $this->success('添加成功！', 'test/teacherlist');
            } else {
                $this->error($result);
            }
        }
        return view();
    }



    /**
     * 最终监考结果
     */
    public function teacherlistresult()
    {
        $taskListData =  Db::name('task')->where('schoolid', 985)->select();
        $this->assign('taskdata', $taskListData);
        $teacherInfoListData =  Db::name('teacher_info')
            ->select();
        $this->assign('teacherinfodata', $teacherInfoListData);
        return view();
    }

    /**
     * 最终监考结果列表
     */
    public function gettestteacherlist()
    {
        $map['a.schoolid'] = array('eq', 985);
        $offset =  (input('get.page') - 1) * input('get.limit');

        $record =  Db::name('teacher')
            ->alias('a')
            ->join('teacher_info b', 'a.teacherid = b.teacherid', 'LEFT')
            ->join('task c', 'a.taskid = c.id')
            ->field('c.name,b.teachername,a.*')
            ->where($map)->order('a.id')->limit($offset, input('get.limit'))->select();
        $total =  Db::name('teacher')->alias('a')->where($map)->order('id')->select();

        $listData['record'] = $record;
        $listData['total'] = count($total);
        return json($listData);
    }

    /**
     * 添加课程
     */
    public function courselistadd()
    {
        $taskListData =  Db::name('task')->where('schoolid', 985)->select();
        $this->assign('taskdata', $taskListData);
        return view();
    }

    /**
     * 添加课程
     */
    public function getcourseinfo()
    {
        $map['schoolid'] = array('eq', 985);

        $offset =  (input('get.page') - 1) * input('get.limit');
        $record =  Db::name('kecheng')->where($map)->order('id')->limit($offset, input('get.limit'))->select();
        $total =  Db::name('kecheng')->where($map)->order('id')->select();

        $listData['record'] = $record;
        $listData['total'] = count($total);
        return json($listData);
    }

    public function courseadd()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $newdata = array();
            // var_dump();
            $ids = json_decode($data['ids'], true);

            for ($i = 0; $i < count($ids); $i++) {

                $newdata[]  = [
                    'schoolid' => 985,
                    'kechengid' => $ids[$i],
                    'taskid' => $data['taskid'],
                    'demo' => '考试课程',       // 备注
                    'create_time' => date('y-m-d h:i:s', time()),
                ];
            }

            $result =  Db::name('course')->insertAll($newdata);


            if ($result !== false) {
                $this->success('添加成功！', 'test/courselist');
            } else {
                $this->error($result);
            }
        }
        return view();
    }

    /**
     * 添加监考老师
     */
    public function teacherlistadd()
    {
        $taskListData =  Db::name('task')->where('schoolid', 985)->select();
        $this->assign('taskdata', $taskListData);
        $teacherInfoListData =  Db::name('teacher_info')
            ->select();
        $this->assign('teacherinfodata', $teacherInfoListData);
        return view();
    }

    public function teacheradd()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $newdata = array();
            // var_dump();
            $ids = json_decode($data['ids'], true);

            for ($i = 0; $i < count($ids); $i++) {

                $newdata[]  = [
                    'schoolid' => 985,
                    'teacherid' => $ids[$i],
                    'taskid' => $data['taskid'],
                    'demo' => '监考老师',       // 备注
                    'create_time' => date('y-m-d h:i:s', time()),
                    'uid' => $data['uid'],
                    'role' => 1,
                ];
            }

            $result =  Db::name('teacher')->insertAll($newdata);

            if ($result !== false) {
                $this->success('添加成功！', 'test/teacherlist');
            } else {
                $this->error($result);
            }
        }
        return view();
    }

    // 编辑
    public function teacheredit()
    {
        if (Request::instance()->isGet()) {
            $result =  Db::name('teacher')->find(input('get.id'));
            if ($result !== false) {
                $this->success('成功！', '',  $result);
            } else {
                $this->error($result);
            }
        }

        if (Request::instance()->isPost()) {
            $data = [
                'id' => input('post.id'),
                'teacherid' => input('post.teacherid'),
                'taskid' => input('post.taskid'),
                'role' => input('post.role'),
                'demo' => input('post.demo'),
            ];

            $result = Db::name('teacher')
                ->where('id', $data['id'])
                ->update(['teacherid' => $data['teacherid'], 'taskid'
                => $data['taskid'], 'role' => $data['role'], 'demo' => $data['demo']]);

            if ($result !== false) {
                $this->success('编辑成功！', 'test/teacherlist');
            } else {
                $this->error($result);
            }
        }
        return view();
    }

    // 删除
    public function teacherremove()
    {
        if (Request::instance()->isGet()) {
            $res =  Db::name('teacher')->where('id', input('get.id'))
                ->delete();
            $res2 = Db::name('test_apply')->where('teacherid', input('get.teacherid'))
                ->delete();

        } else {
            $ids = input('post.ids');
            $res = Db::name('teacher')->delete($ids);
            // $res2 = Db::name('test_apply')->delete($ids);
        }
return;
        if ($res) {
            $this->success('删除成功！', 'test/teacherlist');
        } else {
            $this->error('删除失败');
        }
    }

    /* 监考结果模块 */
    public function resultlist()
    {
        $listData =  Db::name('result')
            ->alias('a')
            ->join('teacher_info b', 'a.teacherid = b.teacherid')
            ->join('task c', 'a.taskid = c.id')
            ->join('kecheng d', 'a.kechengid = d.kechengid')
            ->join('classroom e', 'a.roomid = e.id')
            ->field('c.name,b.teachername,d.coursename,e.roomname,a.*')
            ->where('a.schoolid', 985)->order('a.id')->select();

        $this->assign('dblistdata', $listData);

        // 排监考页面数据获取
        $taskListData =  Db::name('task')->where('schoolid', 985)->select();
        $this->assign('taskdata', $taskListData);

        // 筛选参加监考的老师
        $teacherInfoListData =  Db::name('teacher_info')
            ->alias('a')
            ->join('teacher b', 'a.teacherid = b.teacherid')
            ->field('b.teacherid as passteacherid,b.schoolid,a.*')
            ->where('b.schoolid', 985)->select();
        $this->assign('teacherinfodata', $teacherInfoListData);

        // 筛选985且审核通过的课程
        $map['a.schoolid']  = array('eq', 985);
        $map['a.check']  = array('eq', 1);
        $kechengListData =  Db::name('course')
            ->alias('a')
            ->join('kecheng b', 'a.kechengid = b.kechengid')
            ->field('b.coursename,a.*')
            ->where($map)->select();
        $this->assign('kechengdata', $kechengListData);

        $classroom =  Db::name('classroom')->where('schoolid', 985)->select();
        $this->assign('classroom', $classroom);

        $taskListData =  Db::name('task')->where('schoolid', 985)->select();
        $this->assign('taskdata2', $taskListData);

        return view();
    }

    public function getresultlist()
    {
        $map['a.schoolid'] = array('eq', 985);
        $offset =  (input('get.page') - 1) * input('get.limit');

        input('get.id') && $map['a.id']  = array('eq', input('get.id'));
        input('get.name') && $map['c.name']  = array('like', '%' . input('get.name') . '%');
        input('get.teachername') && $map['b.teachername']  = array('like', '%' . input('get.teachername') . '%');
        input('get.coursename') && $map['d.coursename']  = array('like', '%' . input('get.coursename') . '%');
        input('get.task_date') && $map['a.task_date']  = array('like', '%' . input('get.task_date') . '%');

        $record =  Db::name('result')
            ->alias('a')
            ->join('teacher_info b', 'a.teacherid = b.teacherid')
            ->join('task c', 'a.taskid = c.id')
            ->join('kecheng d', 'a.kechengid = d.kechengid')
            ->join('class e', 'a.class_id = e.id')
            ->join('classroom f', 'a.roomid = f.id')
            ->field('c.name,b.teachername,d.coursename, e.class_name, f.roomname,a.*')
            ->where($map)->order('id')->limit($offset, input('get.limit'))->select();

        // 更新监考次数
        $listData =  Db::name('teacher')->where('schoolid', 985)->select();
        for ($i = 0; $i < count($listData); $i++) {
            $map2['schoolid'] = array('eq', 985);
            $map2['taskid'] = array('eq', $listData[$i]['taskid']);
            $map2['teacherid'] = array('eq', $listData[$i]['teacherid']);
            $teacherNum =  Db::name('result')->where($map2)->select();
            $teacherIds = Db::name('teacher')
                ->where($map2)->update(['join_num' => count($teacherNum)]);
        }
        $total =  Db::name('result')->where('schoolid', 985)->order('id')->select();
        $listData2['record'] = $record;
        $listData2['total'] = count($total);

        return json($listData2);
    }

    public function resultadd()
    {
        if (request()->isAjax()) {
            $data = input('post.');

            $validate = new TestValidate();

            if (!$validate->scene('resultadd')->check($data)) {
                return $validate->getError();
            }

            $result =  Db::name('result')->insert($data);
            if ($result !== false) {
                $this->success('操作成功！', '');
            } else {
                $this->error($result);
            }
        }
        // return view();
    }

    // 编辑
    public function resultedit()
    {
        if (Request::instance()->isGet()) {
            $result =  Db::name('result')->find(input('get.id'));
            if ($result !== false) {
                $this->success('操作成功！', '',  $result);
            } else {
                $this->error($result);
            }
        }

        if (Request::instance()->isPost()) {
            $data = [
                'id' => input('post.id'),
                'teacherid' => input('post.teacherid'),
                'taskid' => input('post.taskid'),
                'kechengid' => input('post.kechengid'),
                'roomid' => input('post.roomid'),
                'role' => input('post.role'),
                'address' => input('post.address'),
                'task_date' => input('post.task_date'),
                'begin_time' => input('post.begin_time'),
                'end_time' => input('post.end_time')
            ];

            // $validate = new TestValidate();

            // if (!$validate->scene('resultadd')->check($data)) {
            //     return $validate->getError();
            // }

            $result = Db::name('result')
                ->where('id', $data['id'])
                ->update([
                    'teacherid' => $data['teacherid'],
                    'taskid' => $data['taskid'],
                    'kechengid' => $data['kechengid'],
                    'roomid' => $data['roomid'],
                    'role' => $data['role'],
                    'address' => $data['address'],
                    'task_date' => $data['task_date'],
                    'begin_time' => $data['begin_time'],
                    'end_time' => $data['end_time']
                ]);

            if ($result !== false) {
                $this->success('操作成功！', 'test/resultlist');
            } else {
                $this->error($result);
            }
        }
        return view();
    }
    // 删除
    public function resultremove()
    {

        if (Request::instance()->isGet()) {
            $res =  Db::name('result')->where('id', input('get.id'))
                ->delete();
        } else {
            $ids = input('post.ids');
            $res = Db::name('result')->delete($ids);
        }

        if ($res) {
            $this->success('删除成功！', 'test/resultlist');
        } else {
            $this->error('删除失败');
        }
    }

    // 调监考申请
    public function changeresult()
    {
        if (request()->isAjax()) {
            $data =  input('post.');


            $result =  Db::name('exchange')->insert($data);
            if ($result !== false) {
                $this->success('操作成功！', 'test/changeresultlist');
            } else {
                $this->error($result);
            }
        }
    }

    public function changeresultlist()
    {
        $listData =  Db::name('result')
            ->alias('a')
            ->join('teacher_info b', 'a.teacherid = b.teacherid')
            ->join('task c', 'a.taskid = c.id')
            ->join('kecheng d', 'a.kechengid = d.kechengid')
            ->join('classroom e', 'a.roomid = e.id')
            ->field('a.id as resultid,c.name,b.teachername,d.coursename,e.roomname,a.*')
            ->where('a.schoolid', 985)->order('a.id')->select();

        $this->assign('dblistdata', $listData);

        $teacherInfoListData =  Db::name('teacher_info')->select();
        $this->assign('teacherinfodata', $teacherInfoListData);
        $taskListData =  Db::name('task')->where('schoolid', 985)->select();
        $this->assign('taskdata2', $taskListData);

        // 审核通过更新数据库
        $isPass = Db::name('exchange')->where('check1&check2', '=', 1)->select();
        if ($isPass) {
            foreach ($isPass as $item) {
                Db::name('result')
                    ->where('id', $item['resultid'])
                    ->update(['teacherid' => $item['newteacherid']]);
            }
        }
        return view();
    }

    public function getchangelist()
    {
        $listData =  Db::name('exchange')
            ->alias('a')
            ->join('teacher_info b', 'a.teacherid = b.teacherid')
            ->join('teacher_info e', 'a.newteacherid=e.teacherid')
            ->join('task c', 'a.taskid = c.id')
            // ->join('result d', 'a.resultid = d.id')
            ->field('c.name,b.teachername,b.id as oldid,e.teachername as newteacher,e.id as newid,a.*')
            ->where('a.schoolid', 985)->order('a.id')->select();

        return json($listData);
    }

    // 删除
    public function changeresultremove()
    {
        if (Request::instance()->isGet()) {
            $res =  Db::name('exchange')->where('id', input('get.id'))
                ->delete();
        } else {
            $ids = input('post.ids');
            $res = Db::name('exchange')->delete($ids);
        }

        if ($res) {
            $this->success('删除成功！', 'test/changeresultlist');
        } else {
            $this->error('删除失败');
        }
    }

    // 审核
    public function changecheck()
    {

        if (Request::instance()->isGet()) {
            $result = Db::name('exchange')->find(input('get.id'));
        } else {

            $data = input('post.');
            if (session('userclass') == 1) { // 二级学院审核
                $map = ['check1' =>  $data['check']];
            } else {
                $map = ['check2' =>  $data['check']];
            }

            $result = Db::name('exchange')
                ->where('id', $data['id'])
                ->update($map);

            if ($result !== false) {
                $this->success('操作成功', null);
            } else {
                $this->error($result);
            }
        }

        if ($result !== false) {
            $this->success('操作成功！', "test/changeresultlist", $result);
        } else {
            $this->error($result);
        }
    }

    // 文件上传
    public function save()
    {
        if (request()->isPost()) {
            vendor("PHPExcel.PHPExcel");
            $objPHPExcel = new \PHPExcel();
            //获取表单上传文件
            $file = request()->file('excel');
            //上传验证后缀名,以及上传之后移动的地址  E:\wamp\www\bick\public
            $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public');
            if ($info) {
                //              echo $info->getFilename();
                $exclePath = $info->getSaveName();  //获取文件名
                $file_name = ROOT_PATH . 'public' . DS . $exclePath; //上传文件的地址
                $objReader = \PHPExcel_IOFactory::createReader("Excel2007");
                $obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8
                $excel_array = $obj_PHPExcel->getSheet(0)->toArray();   //转换为数组格式
                array_shift($excel_array);  //删除第一个数组(标题);
                $city = [];
                $i = 0;
                foreach ($excel_array as $k => $v) {
                    $city[$k]['kechengid'] = $v[1];
                    $city[$k]['coursename'] = $v[2];
                    $city[$k]['schoolid'] = $v[3];

                    $i++;
                }
                $result = Db::name("kecheng")->insertAll($city);

                if ($result !== false) {
                    $this->success('操作成功！', "test/courselist");
                } else {
                    $this->error($result);
                }
            } else {
                echo $file->getError();
            }
        }
    }

    /**
     * @获取教室信息表
     */
    public function getclasslist()
    {
        $listData =  Db::name('classroom')
            ->where('schoolid', 985)->order('id')->select();
        return json($listData);
    }

    public function autoSortTest()
    {
        $resultList = json_decode(input('post.resultList'));
        $data = array();
        $data = $resultList; //数组赋值操作；
        foreach ($data as $item => $value) {
            $data2 = [
                // 'id' => input('post.id'),
                'schoolid' => $value->schoolid,
                'kechengid' => $value->kechengid,
                'teacherid' => $value->teacherid,
                'taskid' => $value->taskid,
                'class_id' => $value->class_id,
                'roomid' => $value->roomid,
                'task_date' => $value->task_date,
                'begin_time' => $value->begin_time,
                'end_time' => $value->end_time
            ];
            // var_dump($data2);

            $result =  Db::name('result')->insert($data2);
            // echo $item;
        }


        if ($result !== false) {
            $this->success('操作成功！', '');
        } else {
            $this->error($result);
        }
    }

    public function getClassInfo()
    {
        $listData =  Db::name('class')
            ->where('schoolid', 985)->order('id')->select();
        return json($listData);
    }

    /**
     * 获取首页统计图数据
     */
    public function getIndexInfo()
    {

        $map['schoolid'] = array('eq', 985);
        $total =  Db::name('teacher_info')->where($map)->order('id')->select();
        $teacherTest =  Db::name('teacher')->where($map)->order('id')->select();
        $courseTotal =  Db::name('kecheng')->where($map)->order('id')->select();

        // 通过的课程数
        $map['check'] = array('eq', 1);
        $passCourse =  Db::name('course')->where($map)->order('id')->select();

        $map2['a.schoolid'] = array('eq', 985);

        if (session('userclass') == 1) {
            $map2['a.check'] = array('eq', 0);
            $test_apply =  Db::name('test_apply')
                ->alias('a')
                ->join('teacher_info b', 'a.teacherid = b.id')
                ->join('task c', 'a.taskid = c.id')
                ->Field('b.teachername,c.name, a.*')
                ->where($map2)->order('id')->select();
            $listData['itemTreatment'] = $test_apply;
        } else {
            $map2['a.check'] = array('eq', 0);
            $courseListData =  Db::name('course')
                ->alias('a')
                // ->Field('a.id as aid, a.*')
                ->join('task c', 'a.taskid = c.id')
                ->join('kecheng b', 'a.kechengid = b.kechengid', 'LEFT')
                ->field('c.name,b.id as c_id,b.*,a.*')
                ->where($map2)->order('a.id')->select();

            $exchange =  Db::name('exchange')
                ->alias('a')
                ->join('teacher_info b', 'a.teacherid = b.teacherid')
                ->join('teacher_info e', 'a.newteacherid=e.teacherid')
                ->join('task c', 'a.taskid = c.id')
                ->field('c.name,b.teachername,b.id as oldid,e.teachername as newteacher,e.id as newid,a.*')
                ->where('a.schoolid', 985)->order('a.id')->select();

            $itemTreatment = array_merge($courseListData->toArray(), $exchange->toArray());

            $listData['itemTreatment'] = $itemTreatment;
        }

        $listData['teacherTotal'] = count($total);
        $listData['teacherTest'] = count($teacherTest);
        $listData['courseTotal'] = count($courseTotal);
        $listData['passCourse'] = count($passCourse);
        $listData['userClass'] = session('userclass');
        return json($listData);
    }
}
